package top.mhtools.pigeon.wechat.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.mhtools.pigeon.common.enums.DingMessageTypeEnum;
import top.mhtools.pigeon.wechat.dto.WeChatMessageReqDTO;
import top.mhtools.pigeon.wechat.service.SendMessageWeChatService;
import top.mhtools.pigeon.wechat.service.support.AbstractWeChatMessageExecuteStrategy;
import top.mhtools.pigeon.wechat.service.support.WeChatMessageExecuteChooser;

import java.util.Optional;

/**
 * 企业微信开放平台消息发送
 *
 * @author machen
 * @date 2020/4/22 19:57
 */
@Slf4j
@Service
public class SendMessageWeChatServiceImpl implements SendMessageWeChatService {

    @Autowired
    private WeChatMessageExecuteChooser dingMessageExecuteChooser;

    @Override
    public void sendMessage(WeChatMessageReqDTO weChatMessageReqDTO) {
        String msgType = Optional.ofNullable(weChatMessageReqDTO.getMsgtype())
                .orElse(DingMessageTypeEnum.MD.getName());
        AbstractWeChatMessageExecuteStrategy dingMessageExecuteStrategy = dingMessageExecuteChooser.choose(msgType);
        dingMessageExecuteStrategy.execute(weChatMessageReqDTO);
    }
}
